import service from '../../../common/service';

// const customerObject = service.getStorageSync("CUSTOMEROBJECT");
// const customerNo = JSON.parse(customerObject).EnjoyRDS.c_cloud_id + "_";

//获取名称
const getRelatedName = (key, value) => {
    const map = service.getLocalStorage(key);
    // const map = JSON.parse(service.getLocalStorage(key));
    if (!map) {
        return '';
    }
    return map[value]
}

const getStoreRelatedName = (value) => {
    const map = service.getLocalStorage('rdsStores');
    if (!map) {
        return '';
    }
    return map[value]
}
//获取字典名称
const getDictName = (className, value) => {
    const rdsDict = service.getLocalStorage('rdsDict');
    // const rdsDict = JSON.parse(service.getLocalStorage('rdsDict'));
    if (!rdsDict) {
        return '';
    }
    const dictMap = rdsDict.find(x => x.c_class_name === className && x.c_id === value);
    return dictMap ? dictMap.c_name : ''
}

//获取字典的集合
const getDictList = (className) => {
    const rdsDict = service.getLocalStorage('rdsDict');
    if (!rdsDict) {
        return [];
    }
    const dictMap = rdsDict.filter(x => x.c_class_name === className);
    return dictMap
}

//获取用户字典的集合
const getUserDictList = (className) => {
    const rdsUserDict = service.getLocalStorage('rdsUserDict');
    if (!rdsUserDict) {
        return [];
    }
    const dictMap = rdsUserDict.filter(x => x.c_class_name === className);
    return dictMap
}
const getUserDictName = (className, value) => {
    const rdsUserDict = service.getLocalStorage('rdsUserDict');
    if (!rdsUserDict) {
        return '';
    }
    const dictMap = rdsUserDict.find(x => x.c_class_name === className && x.c_id === value);
    return dictMap ? dictMap.c_name : '';
}

//获取下拉框的集合
const getDictSelectList = (className) => {
    const dictList = getDictList(className);
    let selectList = [];
    dictList?.forEach(({ c_name, c_id }) => {
        selectList.push({
            text: c_name,
            value: c_id
        })
    });
    return selectList;
}

/**
 * 获取下拉框的集合
 * @param {dataItem:字典名称,enableDTIds:启用的字典ID,excludeDTIds:排除的字典ID,enableNullRow:是否启用空行} param0
 * @returns
 */
const getDictSelect = ({ dataItem = '', enableDTIds = [], excludeDTIds = [], enableNullRow = false }) => {
    if (!dataItem) {
        return [];
    }
    const dictList = getDictList(dataItem);
    let selectList = [];
    dictList?.forEach(({ c_name, c_id }) => {
        selectList.push({
            text: c_name,
            value: c_id
        })
    });
    if (enableDTIds && enableDTIds.length > 0) {
        selectList = selectList.filter(x => enableDTIds.includes(x.value));
    }
    if (excludeDTIds && excludeDTIds.length > 0) {
        selectList = selectList.filter(x => !excludeDTIds.includes(x.value));
    }
    if (enableNullRow) {
        selectList.unshift({ text: '', value: '' });
    }
    return selectList;
}


//获取用户下拉框的集合
const getUserDictSelectList = (className) => {
    const dictList = getUserDictList(className);
    let uniqueArr = dictList.reduce((accumulator, current) => {
        if (!accumulator.some((item) => item.c_name === current.c_name)) {
            accumulator.push(current);
        }
        return accumulator;
    }, []);

    let selectList = [];
    uniqueArr.forEach(({ c_name, c_id }) => {
        selectList.push({
            text: c_name,
            value: c_id
        })
    });
    return selectList;
}

//获取审核状态字典名称
const getRdsAuditStatusDict = (value) => {
    const dictList = service.getLocalStorage('s_gl_bill_status');
    if (!dictList) {
        return '';
    }
    const dictMap = dictList.filter(x => x.c_id === value);
    return dictMap.length < 1 ? '' : dictMap[0]["c_name"]
}

//获取费用项目名称
const getFeeItemName = (value) => {
    const feeItemDict = service.getLocalStorage('rdsFeeItem');
    if (!feeItemDict) {
        return '';
    }
    return feeItemDict[value] || '';
}

//获取采购基地名称
const getFreshLocationName = (value) => {
    const dict = service.getLocalStorage('rdsFreshLocation');
    if (!dict) {
        return '';
    }
    return dict.find(x => x.c_no === value)?.c_name || '';
}

const getDataRuleName = (value) => {
    const dict = service.getLocalStorage('rdsDataRule');
    if (!dict) {
        return '';
    }
    return dict[value] || '';
}

const getLateFeeRuleName = (value) => {
    const dict = service.getLocalStorage('rdsLateFeeRule');
    if (!dict) {
        return '';
    }
    return dict[value] || '';
}


/**
 * 缓存名称配置
 */
const storageNameMap = {
    '巡检时间段': 'rdsPatrolPeriod',
    '费用项目': 'rdsFeeItem',
    '物业服务类型': 'rdsServiceType',
    '巡检主题清单': 'rdsPartolSubject',
    '物业服务项目': 'rdsPServiceItem',
}
/**
 * 获取其他缓存数据名称
 * @param {*} storageName
 * @param {*} code
 * @returns
 */
const getOtherStorageName = (storageName, code) => {
    const storageData = service.getLocalStorage(storageNameMap[storageName]);
    if (!storageData) {
        return '';
    }
    return storageData.find(x => x.c_id === code)?.c_name || '';
}

/**获取其他缓存数据下拉框的集合
 * @param storageName:缓存名称
 * @returns
 * */
const getOtherStorageSelectList = (storageName, type) => {
    const dictList = service.getLocalStorage(storageNameMap[storageName]);
    console.log('dictList', dictList)
    if (!dictList) {
        return [];
    }
    let uniqueArr = [];
    if (type){
        uniqueArr = dictList.filter(x =>x.c_type===type || x.c_task_type === type)?.reduce((accumulator, current) => {
            if (!accumulator.some((item) => item.c_name === current.c_name)) {
                accumulator.push(current);
            }
            return accumulator;
        }, []);
        console.log('uniqueArr', uniqueArr)
    }
    else {
        uniqueArr = dictList?.reduce((accumulator, current) => {
            if (!accumulator.some((item) => item.c_name === current.c_name)) {
                accumulator.push(current);
            }
            return accumulator;
        }, []);
    }

    let selectList = [];
    uniqueArr.forEach(({ c_name, c_id }) => {
        selectList.push({
            text: c_name,
            value: c_id
        })
    });
    return selectList;
}
const getWeightUnits = () => {
    const weightItemDict = service.getLocalStorage('rdsWeightUnits');
	console.log('weightItemDict',weightItemDict)
    if (!weightItemDict) {
        return [];
    }
    return weightItemDict;
}
export {
    getRelatedName,
    getStoreRelatedName,
    getDictName,
    getDictList,
    getDictSelect,
    getDictSelectList,
    getRdsAuditStatusDict,
    getUserDictList,
    getUserDictName,
    getUserDictSelectList,
    getFeeItemName,
    getOtherStorageName,
    getOtherStorageSelectList,
	getWeightUnits,
    getFreshLocationName,
    getDataRuleName,
    getLateFeeRuleName
}
